package ftp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;
import java.util.Properties;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

import org.apache.log4j.Logger;

public class CopyOfFtpCharge {

	private Logger log = Logger.getLogger(CopyOfFtpCharge.class);
	private String ftpDir1 = "/crm/crmwk/wanggy/audit";
	private String ftpDir2 = "/crm/crmwk/wanggy/audit_up";
	private String filePath = "d:/audit_ftp";

	public static String getValue(String key) {
		Properties pro = new Properties();
		InputStream inputFile;
		String value = null;
		try {
			inputFile = new FileInputStream("src/ftp/ftp.properties");
			pro.load(inputFile);
			if (pro.containsKey(key)) {
				value = pro.getProperty(key);
			}
			inputFile.close();
		} catch (FileNotFoundException e) {
			System.out.println("读取属�性文件失败原因：文件路径错误或者文件不存在");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("装载文件--->失败!");
			e.printStackTrace();
		}
		return value;
	}

	public void ftpCharge() {
		FTPClient ftpClient = new FTPClient();
		FTPFile[] fileList = null;

		// 下载到本地服务器
		try {
			ftpClient.connect(getValue("ftp1ip"),21);
			ftpClient.login(getValue("ftp1userName"), getValue("ftp1password"));
			 log.info("登录"+getValue("ftp1ip")+"成功");

			fileList = ftpClient.listFiles(new String(ftpDir1.getBytes("GBK"),
					"iso-8859-1"));
			System.out.print(fileList.length);
			for (int i = 0; i < fileList.length; i++) {
				String name = fileList[i].getName();
				File file = new File("d:/audit_ftp/" + name);
				// FileOutputStream fos = new FileOutputStream(new
				// File(filePath+"/"+name));
				FileOutputStream fos = new FileOutputStream(file);
				boolean flag = ftpClient.retrieveFile(ftpDir1+"/"+name, fos);
				fos.close();
				if (flag) {
					System.out.print("删除"+ftpClient.deleteFile(ftpDir1+"/"+fileList[i].getName()));
					log.info(" download success !!! ");
				}
			}
			ftpClient.disconnect();
			// log.info(" disconnect " + getValue("ftp1ip") + "success !!! ");
		} catch (SocketException e) {
			log.info(" SocketException ");
			e.printStackTrace();
		} catch (IOException e) {
			log.info("IOException ");
			e.printStackTrace();
		}

		// 上传到服务器

		try {
			ftpClient.connect(getValue("ftp2ip"));
			ftpClient.login(getValue("ftp2userName"), getValue("ftp2password"));
			// log.info("登录" + getValue("ftp2ip") + "成功");

			File file = new File("d:/audit_ftp/");
			String[] filelist = file.list();
			for (int i = 0; i < filelist.length; i++) {
				File readfile = new File(filePath + "\\" + filelist[i]);
				System.out.print(" 文件"+readfile);
				if (!readfile.isDirectory()) {
					String name = readfile.getName();
					InputStream fos = new FileInputStream(filePath + "/" + name);
					System.out.println(ftpClient.storeUniqueFile(ftpDir2 + "/"
							+ name, fos));
					fos.close();
				}
				System.out.println("delete" + readfile.delete());
			}

		} catch (SocketException e) {
			log.info(" SocketException ");
			e.printStackTrace();
		} catch (IOException e) {
			log.info("IOException ");
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {

		new CopyOfFtpCharge().ftpCharge();
	}

}
